
var SERVER_PORT = "http://120.77.223.88:8080/red/";

var userCenter = new Vue({
    el: '#app-userCenter',
    data: {
        //登录账户
        isLogin: false,
        phone: "",
        nickname: "",
        
        //得到所有目录
        primaryCategory: [],
    },
    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);
        },

        getAllCategory: function () {
            this.$http.get(SERVER_PORT + "requestCategory").then(
                response => {
                    //console.log(response.data.data);
                    var originalChaoticInfo = response.data.data;

                    for (var index in originalChaoticInfo) {
                        var primaryCategoryName = originalChaoticInfo[index].category1_Name;
                        var primaryCategoryID = originalChaoticInfo[index].category1_ID;

                        var primaryCategoryItem = {
                            name: primaryCategoryName,
                            ID: primaryCategoryID
                        }

                        // var secondCategory = [];

                        // for (var sub_index in originalChaoticInfo[index].category2) {
                        //     var secondCategoryName =
                        //         originalChaoticInfo[index].category2[sub_index].name;
                        //     var category2_ID =
                        //         originalChaoticInfo[index].category2[sub_index].category2_ID;
                        //     secondCategory.push({
                        //         name: secondCategoryName,
                        //         ID: category2_ID
                        //     });
                        // }

                        // this.categoryTree.push({
                        //     primaryCategory: primaryCategory,
                        //     secondCategory: secondCategory
                        // });
                        this.primaryCategory.push(primaryCategoryItem);
                    }

                },
                response => {
                    // 响应错误回调
                    alert("请求失败");
                }
            );
        },

        onPrimCateClick: function (index) {
            console.log(index);
            window.localStorage.setItem("primIndex_goodCart", index);
            window.location.href = "searchResult.html"
        },


    },
    created: function () {

        this.getAllCategory();
        if(window.localStorage.getItem("isLogin") === undefined || window.localStorage.getItem("isLogin") === "false"){
            alert("请先登录");
            window.location.href="login.html";
        }else{
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
        }
    }
});